<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Transaksi extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('ExCRUD');
        $this->load->library('session');
        $this->load->model('member_model');
        $crud = new Excrud();
        $crud->standartCheck();        
    }
        
    
    public function deposit(){        
        $saldoTerkini = $this->member_model->getSaldoTerkini();
        $data['saldo'] = $saldoTerkini;
        $data['kepemilikan_port'] = $this->member_model->get_kepemilikan_port();        
        $this->load->view('header/header_member.php', $data);
        $this->load->view('transaksi/deposit.php', $data);
    }
    
    public function request_deposit(){
        $this->load->model('request_transaksi_model');
        
        $nominal_transfer = intval(strip_tags($this->input->post('nominal_transfer')));
        $pilihan_bank = strip_tags($this->input->post('pilihan_bank'));
        
        if ($nominal_transfer < 10000) {
            $rc = "20";
            $resp = "Nominal transfer minimal Rp. 10.000,- (Sepuluh Ribu Rupiah)";
        } else if ($pilihan_bank === "") {
            $rc = "20";
            $resp = "Silahkan pilih bank terlebih dahulu";
        } else {
            $wording_deposit = $this->request_transaksi_model->request_deposit($nominal_transfer, $pilihan_bank);
            $rc = "00";            
            $resp = $wording_deposit;
        }
        echo json_encode(array("err_code" => $rc, "msg" => $resp));
    }

    public function smskit() {
        
        $this->load->model('produk_model');
        $saldoTerkini = $this->member_model->getSaldoTerkini();
        $harga = $this->produk_model->getHarga('SMSKIT');
        $data = array();
        $data['jenis_request'] = 'smskit';
        $data['saldo'] = $saldoTerkini;
        $data['harga'] = $harga;
        $data['error'] = mysql_real_escape_string($this->input->get('error'));
        
        $data['kepemilikan_port'] = $this->member_model->get_kepemilikan_port();        
        $this->load->view('header/header_member.php', $data);
        $this->load->view('transaksi/smskit_new.php', $data);
    }

    public function pre_submit_smskit() {
        
        $this->load->model('produk_model');
        $this->load->model('request_transaksi_model');
        $penerima = strip_tags($this->input->post('penerima'));        
        $penerima = str_replace("\n", "*", $penerima);
        $jumlahPesan = count(explode("*",$penerima));        
        $isiPesan = strip_tags($this->input->post('isi_pesan'));
        $saldoTerkini = $this->member_model->getSaldoTerkini();
        
        
        $harga = $this->produk_model->getHarga('SMSKIT');
        if (($saldoTerkini - ($jumlahPesan * $harga)) < 0) {
            $rc = "20";
            $resp = "Maaf saldo anda tidak mencukupi, silahkan kurangi jumlah pesan yang akan anda kirim (saldo terkini " . number_format($saldoTerkini, 0, ',', '.') . " = " . floor($saldoTerkini / $harga) . " SMS)";
        } else if ($jumlahPesan === "") {
            $rc = "20";
            $resp = "Jumlah pesan tidak boleh kosong";
        } else if ($isiPesan === "") {
            $rc = "20";
            $resp = "Isi pesan tidak boleh kosong";
        } else {
            $this->session->set_userdata('id_request', $this->request_transaksi_model->tambah_request_smskit($jumlahPesan, $isiPesan, 'SMSKIT', $penerima));
            $this->session->set_userdata('total_transaksi', $jumlahPesan * $harga);
            $rc = "00";
            $resp = "--- Tekan tombol Oke, dan tunggu proses upload selesai ---";
        }
        echo json_encode(array("err_code" => $rc, "msg" => $resp));
    }

    function do_upload_smskit() {
        $config['upload_path'] = './file_upload/';
        $config['allowed_types'] = '*';
        $config['max_size'] = '1000';
        $config['remove_spaces'] = TRUE;
        $new_name = $this->session->userdata('id_request') . '-' . $_FILES["userfile"]['name'];
        $config['file_name'] = $new_name;
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload()) {
            if (isset($_POST)) {
                $this->load->model('request_transaksi_model');
                $this->request_transaksi_model->deleteRequestTransaksi($this->session->userdata('id_request'));
                
                $this->member_model->updateSaldo((-1 * $this->session->userdata('total_transaksi')));
            }
            unset($_POST);
            $error = array('error' => $this->upload->display_errors());
            $error = strip_tags($error['error']);
            echo"<script>window.location.href='" . site_url('transaksi/smskit?error=' . $error) . "';</script>";
        } else {
            unset($_POST);
            $data = array('upload_data' => $this->upload->data());
            $this->load->model('request_transaksi_model');
            $this->request_transaksi_model->updateBillInfo($this->session->userdata('id_request'), '2', $data['upload_data']['file_name']);
            $this->request_transaksi_model->updateStatus($this->session->userdata('id_request'),0);
            echo"<script>
                                 alert('Transaksi berhasil dilakukan, silahkan masuk kedalam menu monitoring transaksi untuk memantau sms yang berhasil dikirim');
                                 window.location.href='" . site_url('transaksi/smskit?status=success') . "';
                             </script>";
        }
    }

    public function smsfal() {
        $crud = new Excrud();
        $crud->standartCheck();

        
        $this->load->model('produk_model');
        $this->load->model('kota_model');
        $saldoTerkini = $this->member_model->getSaldoTerkini();
        $harga = $this->produk_model->getHarga('SMSFAL');
        $data = array();
        $data['jenis_request'] = 'smsfal';
        $data['saldo'] = $saldoTerkini;
        $data['harga'] = $harga;
        $data['data_kota'] = $this->kota_model->getDataKota();
        
        $data['kepemilikan_port'] = $this->member_model->get_kepemilikan_port();        
        $this->load->view('header/header_member.php', $data);
        $this->load->view('transaksi/smsfal.php', $data);
    }

    public function pre_submit_smsfal() {        
        
        $this->load->model('produk_model');
        $this->load->model('request_transaksi_model');
        $jumlahPesan = strip_tags($this->input->post('jumlah_pesan'));
        $isiPesan = strip_tags($this->input->post('isi_pesan'));
        $kotaTarget = strip_tags($this->input->post('kota'));
        $saldoTerkini = $this->member_model->getSaldoTerkini();
        $harga = $this->produk_model->getHarga('SMSFAL');
        if (($saldoTerkini - ($jumlahPesan * $harga)) < 0) {
            $rc = "20";
            $resp = "Maaf saldo anda tidak mencukupi, silahkan kurangi jumlah pesan yang akan anda kirim (saldo terkini " . number_format($saldoTerkini, 0, ',', '.') . " = " . floor($saldoTerkini / $harga) . " SMS)";
        } else if ($jumlahPesan === "") {
            $rc = "20";
            $resp = "Jumlah pesan tidak boleh kosong";
        } else if ($isiPesan === "") {
            $rc = "20";
            $resp = "Isi pesan tidak boleh kosong";
        } else if ($kotaTarget === "") {
            $rc = "20";
            $resp = "Kota tidak boleh kosong";
        } else {
            $this->request_transaksi_model->tambah_request_smsfal($jumlahPesan, $isiPesan, $kotaTarget, 'SMSFAL');
            $rc = "00";
            $resp = "--- Transaksi berhasil dilakukan silahkan memonitoring transaksi anda pada menu monitoring transaksi ---";
        }
        echo json_encode(array("err_code" => $rc, "msg" => $resp));
    }

    public function smsos() {
        $crud = new Excrud();
        $crud->standartCheck();
        
        $data['kepemilikan_port'] = $this->member_model->get_kepemilikan_port();        
        $this->load->view('header/header_member.php', $data);
        $this->load->view('transaksi/smsos.php');
    }

    public function profil() {

        
        $this->load->model('produk_model');
        $saldoTerkini = $this->member_model->getSaldoTerkini();

        $data = array();
        $data['saldo'] = $saldoTerkini;
        $data['error'] = mysql_real_escape_string($this->input->get('error'));

        $data['kepemilikan_port'] = $this->member_model->get_kepemilikan_port();        
        $this->load->view('header/header_member.php', $data);
        $this->load->view('transaksi/profil.php', $data);
    }

    public function pre_update_profil() {
        
        $sandi_baru = strip_tags($this->input->post('sandi_baru'));
        $confirm_sandi_baru = strip_tags($this->input->post('confirm_sandi_baru'));
        if ($sandi_baru != $confirm_sandi_baru) {
            $resp = "Konfirmasi Sandi Baru tidak cocok";
        } else if ($sandi_baru === "") {
            show_alert("Sandi Baru tidak boleh kosong");
            } else {
            $this->session->set_userdata('sandi_baru', $sandi_baru);
            $rc = "00";
            $resp = "--- Tekan tombol Oke, dan tunggu proses upload selesai ---";
        }
        echo json_encode(array("err_code" => $rc, "msg" => $resp));
    }

    function do_update_profil() {
        $sandi_baru = $this->session->userdata('sandi_baru');
        if (isset($_POST)) {
            
            $this->member_model->updatePassword($sandi_baru);
            echo"<script>
                         window.location.href='" . site_url('transaksi/profil?status=success') . "';
                     </script>";
        }
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
